package com.mp.approvalreq.servlet;


import com.alibaba.fastjson.JSONArray;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.entity.TemplateComponent;
import com.mp.approvalreq.service.SubmitRequestServicePrx;
import com.mp.approvalreq.util.ApprovalUtils;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.message.Message;
import com.plusmoney.message.SuccessMsg;
import com.plusmoney.util.ParameterUtils;
import com.plusmoney.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 已迁移到GetRequestFlows
 */
@Deprecated
@WebServlet("/getApprovalFlow")
public class GetApprovalFlow extends AbstractServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			User user = getReqUser(req);

			Request request = ParameterUtils.getObjectParameter(req, Request.class);
			check(request);

			initComponent(request);

			SubmitRequestServicePrx submitRequestService = ServiceUtils.getSubmitRequestServicePrx();
			String approvalFlow = submitRequestService.getApprovalFlow(user, request);
			Message.printJson(resp, SuccessMsg.getDefaultMsg(approvalFlow));
		} catch (Throwable e) {
			onThrowable(req, resp, e);
		}
	}

	private void initComponent(Request request) {
		request.setComponentsContent(JSONArray.parseArray(request.getComponentsContentsStr(), TemplateComponent.class));
		request.setComponentsContentsStr(null);
	}

	private void check(Request request) {
		if (Utils.isNull(request)) {
			throw new RpcServerLogicalException(1, "参数不能为空");
		}
		if (request.getTemplateId() == 0) {
			throw new RpcServerLogicalException(1, "模板id不能为空");
		}
	}


}
